<?php
//
// +----------------------------------------------------------------------+
// | PHP version 5.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004                                                   |
// | Moocky Mark(木目子).                                                 |
// | Last Modify:2005-07-09                                               |
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Authors:   Moocky Mark                                               |
// | QQ:        44333697                                                  |
// | MSN:       moocky@hotmail.com                                        |
// | Email:     moocky@moocky.net                                         |
// +----------------------------------------------------------------------+
//
//

/**
* 图片处理部分
*
*
* @version  v 1.5
* @access   Upload
*/
class UploadImg{
  const UPLOAD_EXIST       = -5;    /*目标文件存在*/
  const UPLOAD_SIZE_OVER   = -4;    /*体积超出*/
  const UPLOAD_WIDTH_OVER  = -3;    /*宽度超出*/
  const UPLOAD_HEIGHT_OVER = -2;    /*高度超出*/
  const UPLOAD_UNTYPE      = -1;    /*不存在的类型*/
  const UPLOAD_FAILURE     = 0;     /*上传失败*/
  const UPLOAD_SUCCESS     = 1;     /*上传成功*/
  
  const SRC_NO_EXIST       = -1;    /*源图片不存在或者不是图片文件*/
  const DST_NO_EXIST       = -2;    /*目标图片不存在*/
  
  const MK_NO_SMALL        = -3;
  const MK_SMALL_FAILURE   = 0;
  const MK_SMALL_SUCCESS   = 1;
  
  const WATER_NO_MARK      = -1;    /*加文字水印*/
  const WATER_MARK_TEXT    = 0;     /*加文字水印*/
  const WATER_MARK_IMAGE   = 1;     /*加图片水印*/

  private $mimePhoto       = array();
  private $srcPath         = '';           /*源路经*/
  private $dstPath         = '';           /*目标路经*/
  private $MaxSize         = 2000;
  private $waterString     = 'PATEO.COM.CN'; /*英文或UTF-8*/
  private $waterImage      = '';
  private $font_size       = 5;
  private $iWaterMark      = 1;            /*0.加文字水印；1.图片水印*/
  private $width           = 120;
  private $height          = 140;
  private $maxWidth        = 0;            /*0为不限制*/
  private $maxHeight       = 0;
  private $filename        = '';           /*上传成功以后的文件名*/
  public  $Fixed         = true;           /*是否加边框*/

  public function __construct(){
    /*这里机会包含所有的jpg、png、gif头信息*/
    $this->add_mime(array(
            'image/x-up-wpng'   =>'png',
            'image/x-up-png'    =>'png',
            'image/x-wpng'      =>'png',
            'image/x-png'       =>'png',
            'image/png'         =>'png',

            'image/x-up-wgif'   =>'gif',
            'image/x-up-gif'    =>'gif',
            'image/x-gif'       =>'gif',
            'image/gif'         =>'gif',

            'image/x-up-wpjpeg' =>'jpg',
            'image/x-up-pjpeg'  =>'jpg',
            'image/x-pjpeg'     =>'jpg',
            'image/pjpeg'       =>'jpg',

            'image/x-up-wjpeg'  =>'jpg',
            'image/x-up-jpeg'   =>'jpg',
            'image/x-jpeg'      =>'jpg',
            'image/jpeg'        =>'jpg',

            'image/x-up-wjpe'   =>'jpg',
            'image/x-up-jpe'    =>'jpg',
            'image/x-jpe'       =>'jpg',
            'image/jpe'         =>'jpg',

            'image/x-up-wjpg'   =>'jpg',
            'image/x-up-jpg'    =>'jpg',
            'image/x-jpg'       =>'jpg',
            'image/jpg'         =>'jpg',
            ));
  }
 /**
  * 上传文件
  * @param array:上传文件的系统数据$_FILES['name'];
  * @return string;
  * 返回值所代表的意思：
  * self::UPLOAD_EXIST       //目标文件存在
  * self::UPLOAD_SIZE_OVER   //体积超出
  * self::UPLOAD_WIDTH_OVER  //宽度超出
  * self::UPLOAD_HEIGHT_OVER //高度超出
  * self::UPLOAD_UNTYPE      //不存在的类型
  * self::UPLOAD_FAILURE     //上传失败
  * self::UPLOAD_SUCCESS     //上传失败
  */
  public function upload($file,$dstImage=''){
  	if(!isset($file) || !is_array($file) || $file['size'] == 0)
  		return;

    if($file['size'] > $this->MaxSize*1024)
      return self::UPLOAD_SIZE_OVER;

    $tmpImgInfo = @getimagesize($file['tmp_name']);
    if($this->maxWidth && $this->maxWidth < $tmpImgInfo[0])
      return self::UPLOAD_WIDTH_OVER;

    if($this->maxHeight && $this->maxHeight < $tmpImgInfo[1])
      return self::UPLOAD_HEIGHT_OVER;

    if(!isset($this->mimePhoto[$file['type']]) || $this->mimePhoto[$file['type']]=='')
      return self::UPLOAD_UNTYPE;

    /*重新给源路径和目标路径付值*/
    $this->srcPath = $this->srcPath=='' ? $this->dstPath : $this->srcPath;
    $this->dstPath = $this->dstPath=='' ? $this->srcPath : $this->dstPath;
    if(''!= $dstImage)
      $filename = $dstImage.'.'.$this->mimePhoto[$file['type']];
    else
      $filename = MyPublic::make_rand(16).'.'.$this->mimePhoto[$file['type']];
    /*如果该文件名存在,重新生成一个*/
    if(@file_exists($this->dstPath.'/'.$filename) && $dstImage!='')
      return self::UPLOAD_EXIST;
    elseif(@file_exists($this->dstPath.'/'.$filename))
      return $this->upload($file);

    if(!move_uploaded_file($file['tmp_name'],$this->dstPath.'/'.$filename))
      return self::UPLOAD_FAILURE;
    else{
    	@chmod($this->dstPath.'/'.$filename,0777);
      $this->filename = $filename;
      return self::UPLOAD_SUCCESS;
    }
  }
 /**
  * 获取上传以后的文件名;
  * @param void;
  * @return string;
  */
  public function getFileName(){
    return $this->filename;
  }
 /**
  * 增加上传文件类型
  * @param array/string:上传文件的类型(mime);
  * @param string:扩展名;
  * @return void;
  */
  public function add_mime($mime,$suffix=null){
    if(is_array($mime)){
      foreach($mime as $k => $v)
        $this->mimePhoto[$k]= $v;
    }else{
      $this->mimePhoto[$mime]= $suffix;
    }
  }

 /**
  * 给图片加水印
  * @param string:源图片;
  * @param string:目标图片名;
  * @return int;
  * 只有jpg图片才可以加水印
  */
  public function watermark($srcImage,$dstImage =''){
    /*得到图片信息*/
    $info= $this->getInfo($this->srcPath.'/'.$srcImage);

    if(!$info)
      return self::SRC_NO_EXIST;

    $srcFile = $this->srcPath.'/'.$srcImage;
    $dstFile = $dstImage!='' ? $this->dstPath.'/'.$dstImage : $srcFile;

    /*创建一个黑边白底的图框*/
    $nimage = @imagecreatetruecolor($info['width'],$info['height']);
    $white  = @imagecolorallocate($nimage,255,255,255);
    $black  = @imagecolorallocate($nimage,0,0,0);
    switch($info['ext']){
      case 'gif':
        $func1 = 'imagecreatefromgif';
        $func2 = 'imagegif';
        break;
      case 'jpg':
        $func1 = 'imagecreatefromjpeg';
        $func2 = 'imagejpeg';
        break;
      case 'png':
        $func1 = 'imagecreatefrompng';
        $func2 = 'imagepng';
        break;
      default:
        return self::WATER_NO_MARK;
    }

    $simage = @$func1($srcFile);
    if($simage){
      @imagecopy($nimage,$simage,0,0,0,0,$info['width'],$info['height']);
      if($this->iWaterMark===self::WATER_MARK_TEXT){
        @imagestring($nimage,$this->font_size,$info['width']-109,$info['height']-25,$this->waterString,$black);
        @imagestring($nimage,$this->font_size,$info['width']-110,$info['height']-26,$this->waterString,$white);
        /*如果图片过大再加多个水印*/
        if($info['width'] > 800 && $info['height'] > 600){
          @imagestring($nimage,$this->font_size,$info['width']/2-109,$info['height']/2-25,$this->waterString,$black);
          @imagestring($nimage,$this->font_size,$info['width']/2-110,$info['height']/2-26,$this->waterString,$white);
          @imagestring($nimage,$this->font_size,109,25,$this->waterString,$black);
          @imagestring($nimage,$this->font_size,110,26,$this->waterString,$white);
        }elseif($info['width'] > 400 && $info['height'] > 300){
          @imagestring($nimage,$this->font_size,109,25,$this->waterString,$black);
          @imagestring($nimage,$this->font_size,110,26,$this->waterString,$white);
        }
      }elseif($this->iWaterMark===self::WATER_MARK_IMAGE){
        $wInfo = $this->getInfo($this->waterImage);
        $wimage = @imagecreatefromgif($this->waterImage);
        if($info['width'] > $wInfo['width']*20 && $info['height'] > $wInfo['height']*20){
	        /*如果图片过大再加多个水印*/
          @imagecopymerge($nimage,$wimage,($info['width']-$wInfo['width']*6)/3,($info['height']-$wInfo['height']*6)/3,0,0,$wInfo['width'],$wInfo['height'],30);
          @imagecopymerge($nimage,$wimage,($info['width']+$wInfo['width']*6)/3,($info['height']+$wInfo['height']*6)/3,0,0,$wInfo['width'],$wInfo['height'],30);
        }elseif($info['width'] > $wInfo['width']*2 && $info['height'] > $wInfo['height']*2){
          @imagecopymerge($nimage,$wimage,($info['width']+$wInfo['width'])/4,($info['height']+$wInfo['height'])/4,0,0,$wInfo['width'],$wInfo['height'],40);
        }else{
          /*按照比例水印图片的高、宽*/
          if(($wInfo['width'] / $wInfo['height']) > ($info['width'] / $info['height'])){
            $w = $info['width']/2;
            $h = floor($wInfo['height']*$w / $wInfo['width']);
          }else{
            $h = $info['height']/2;
            $w = floor($wInfo['width']*$h / $wInfo['height']);
          }
          $ni = @imagecreate($w,$h);
          $black = imagecolorallocate($ni, 0, 0, 0);
          @imagecolortransparent($ni,0);
          @imagecopyresampled($ni,$wimage,0,0,0,0,$w,$h,$wInfo['width'],$wInfo['height']);
          @imagecopymerge($nimage,$ni,ceil($info['width']/4),ceil($info['height']/4),0,0,$w,$h,50);
          @imagedestroy($ni);
        }
        @imagedestroy($wimage);
      }
      /*覆盖原上传文件*/
      $result= @$func2($nimage,$dstFile);
      @imagedestroy($nimage);
    }else{
      $result=0;
    }
    @imagedestroy($simage);
    return $result;
  }
 /**
  * 生成一个缩略图
  * @param srcImage:string(源图片);
  * @param dstImage:string(目标图片名);
  * @return string;
  */
  public function make2small($srcImage,$dstImage=''){
    $info= $this->getInfo($this->srcPath.'/'.$srcImage);
    if(!$info)
      return self::SRC_NO_EXIST;

    $srcFile = $this->srcPath.'/'.$srcImage;

    if($dstImage == '')
      $dstImage = MyPublic::make_rand(16).'.'.$this->mimePhoto[$info['mime']];  /*随即生成一个文件名*/

    /*如果该文件已经存在*/
    if(@file_exists($this->dstPath.'/'.$dstImage) && $dstImage !='')
      return self::DST_NO_EXIST;
    elseif(@file_exists($this->dstPath.'/'.$dstImage))
      return $this->make2small($srcImage);

    /*按照比例得到缩略图的高、宽*/
    if(($info['width'] / $info['height']) > ($this->width / $this->height)){
      $w = $this->width;
      $h = floor($info['height']*$w / $info['width']);
    }else{
      $h = $this->height;
      $w = floor($info['width']*$h / $info['height']);
    }
    switch($info['ext']){
      case 'gif':
        $func1 = 'imagecreatefromgif';
        $func2 = 'imagegif';
        break;
      case 'jpg':
        $func1 = 'imagecreatefromjpeg';
        $func2 = 'imagejpeg';
        break;
      case 'png':
        $func1 = 'imagecreatefrompng';
        $func2 = 'imagepng';
        break;
      default:
        return self::MK_NO_SMALL;
    }
    /*创建缩略图*/
    $im= @$func1($srcFile);
    if($im){
      if($this->Fixed){
        $w = ($w >= $this->width -2)?($this->width -2):$w;
        $h = ($h >= $this->height -2)?($this->height -2):$h;
        $ni = @imagecreatetruecolor($this->width,$this->height);
        @imagefilledrectangle($ni,0,0,$this->width,$this->height,0xc8c8c8);
        @imagefilledrectangle($ni,1,1,$this->width-2,$this->height-2,0xffffff);
      }else{
        $ni = @imagecreatetruecolor($w,$h);
      }
      @imagecopyresampled($ni,$im,($this->width-$w)/2,($this->height-$h)/2,0,0,$w,$h,$info['width'],$info['height']);
      $result = @$func2($ni,$this->dstPath.'/'.$dstImage);
      @imagedestroy($ni);
    }

    @imagedestroy($im);

    if(!$result){
      return self::MK_SMALL_FAILURE;
    }else{
      $this->filename = basename($dstImage);
      return self::MK_SMALL_SUCCESS;
    }
  }

 /**
  * 得到图片信息
  * @param string:图片名称;
  * @return array;
  */
  public function getInfo($Image){
    $info= @getimagesize($Image);
    if($info){
      $return            = array();
      $return['width']   = $info[0];
      $return['height']  = $info[1];
      $return['mime']    = $info['mime'];
      $return['ext']     = $this->mimePhoto[$info['mime']];
      $return['channels']= $info['channels'];
      $return['bits']    = $info['bits'];
      return $return;
    }
    return;
  }

 /**
  * 设置源图片路径
  * @param string:$Path;
  * @return void;
  */
  public function setSrcPath($Path){
    $this->srcPath= $Path;
  }
 /**
  * 设置目标图片路径
  * @param string:目标图片路径;
  * @return void;
  */
  public function setDstPath($Path){
    $this->dstPath= $Path;
  }

 /**
  * 设置缩率图大小
  * @param int:宽度;
  * @param int:高度;
  * @return void;
  */
  public function setWH($width,$height){
    $this->width  = $width;
    $this->height  = $height;
  }
 /**
  * 设置上传图片的最大限制;
  * @param int:宽度;
  * @param int:高度;
  * @return void;
  */
  public function setMaxWH($width,$height){
    $this->maxWidth  = $width;
    $this->maxHeight = $height;
  }

 /**
  * 设置加图片水印还是文字水印
  * @param int:水印类型;
  * @return void;
  */
  public function setWaterMark($iWaterMark=2){
    $this->iWaterMark= $iWaterMark;
  }

 /**
  * 设置水印字符(只能为英文或者UTF-8)
  * @param string:水印文字(只能为英文或者UTF-8);
  * @return void;
  */
  public function setWaterString($waterstring){
    $this->waterString= $waterstring;
  }

 /**
  * 设置水印图片路径;
  * @param string:水印图片路径;
  * @return void;
  */
  public function setWaterImage($waterImage){
    $this->waterImage = $waterImage;
  }

 /**
  * 设置上传文件的最大限制;
  * @param int:最大限制;
  * @return void;
  */
  public function setMaxSize($MaxSize){
    $this->MaxSize = $MaxSize;
  }
 /**
  * 获取上传文件的最大限制;
  * @param void;
  * @return int;
  */
  public function getMaxSize(){
    return $this->MaxSize;
  }

 /**
  * 设置水印文字大小
  * @param int:文字大小;
  * @return void;
  */
  public function setFontSize($font_size){
    $this->font_size = $font_size;
  }

 /**
  * 删除资源图片;
  * @param string:图片名称;
  * @return boolean;
  */
  public function delete($Image){
    return @unlink($Image);
  }
 /**
  * 可上传文件的类型
  * @param void;
  * @return array;
  */
  public function getUploadType(){
    return $this->mimePhoto;
  }
}
?>
